﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Chefbook.DataObjects;
using NUnit.Framework;
using Rhino.Mocks;
using Chefbook.DataAccessLayer;
using Chefbook.UserActions;

namespace UnitTests.UserActionsTests
{
    [TestFixture]
    public class AddRecipeToCategoryActionFixture
    {
        private MockRepository mocks;
        private IUserAction action;
        private Category category, newCategory;
        private Recipe recipe;
        private ChefbookEntities db;


        [SetUp]
        public void Init()
        {
            mocks = new MockRepository();
            db = new ChefbookEntities();
            category = new Category();
            category.Name = "OldParent";
            newCategory = new Category();
            newCategory.Name = "NewParent";

            recipe = new Recipe();

            Assert.AreEqual(1, category.DirectRecipes.Count);
            Assert.AreEqual(1, recipe.Categories.Count);
            Assert.True( recipe.Categories.Contains(category));
            Assert.AreEqual(0, newCategory.DirectRecipes.Count);

            action = new AddRecipeToCategoryAction(recipe, newCategory) { DatabaseConnector = db };
        }

        [Test]
        public void DoActionTest()
        {
            action.DoAction();
            Assert.AreEqual(1, category.DirectRecipes.Count);
            Assert.AreEqual(2, recipe.Categories.Count);
            Assert.Contains(category, recipe.Categories.ToList());
            Assert.Contains(newCategory, recipe.Categories.ToList());
            Assert.AreEqual(1, newCategory.DirectRecipes.Count);
        }

        [Test]
        public void UndoActionTest()
        {
            action.DoAction();
            action.UndoAction();
            Assert.AreEqual(1, category.DirectRecipes.Count);
            Assert.AreEqual(1, recipe.Categories.Count);
            Assert.Contains(category, recipe.Categories.ToList());
            Assert.AreEqual(0, newCategory.DirectRecipes.Count);
        }

        [Test]
        public void UndoActionWithoutDoingTest()
        {
            UserActionTestsHelper.UndoActionWithoutDoingTest(action);
        }

        [Test]
        public void UndoActionTwiceAfterDoingTest()
        {
            UserActionTestsHelper.UndoActionTwiceAfterDoingTest(action);
        }

        [Test]
        public void DoActionTwiceTest()
        {
            UserActionTestsHelper.DoActionTwiceTest(action);
        }

        [Test]
        public void ActionNameTest()
        {
            Assert.AreEqual(string.Format("Add Recipe '{0}' To Category '{1}'",recipe, newCategory), action.ToString());
        }
    }
}
